home *** CD-ROM | disk | FTP | other *** search
/ 3D Images / 3D Images.iso / programs / amiga / rayshade / libray / libtext / noise.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-12  |  7.9 KB  |  293 lines

  1. /*
  2.  * noise.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Robert Skinner, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * noise.c,v 4.1 1994/08/09 08:03:13 explorer Exp
  17.  *
  18.  * noise.c,v
  19.  * Revision 4.1  1994/08/09  08:03:13  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:15  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:43:38  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "libcommon/common.h"
  30.  
  31. #define MINX        -1000000
  32. #define MINY        MINX
  33. #define MINZ        MINX
  34.  
  35. #define SCURVE(a) ((a)*(a)*(3.0-2.0*(a)))
  36. #define REALSCALE ( 2.0 / 65536.0 )
  37. #define NREALSCALE ( 2.0 / 4096.0 )
  38. #define Hash3d(a,b,c) hashTable[hashTable[hashTable[(a) & 0xfff] ^ ((b) & 0xfff)] ^ ((c) & 0xfff)]
  39. #define Hash(a,b,c) (xtab[(xtab[(xtab[(a) & 0xff] ^ (b)) & 0xff] ^ (c)) & 0xff] & 0xff)
  40.  
  41. #define INCRSUM(m,s,x,y,z)    ((s)*(RTable[m]*0.5        \
  42.                     + RTable[m+1]*(x)    \
  43.                     + RTable[m+2]*(y)    \
  44.                     + RTable[m+3]*(z)))    \
  45.  
  46.  
  47. #define MAXSIZE 267
  48.  
  49. Float        RTable[MAXSIZE];
  50. static short    *hashTable;
  51. static int    R(), Crc16();
  52.  
  53. static unsigned short xtab[256] =
  54. {
  55.    0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
  56.    0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
  57.    0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
  58.    0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
  59.    0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
  60.    0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
  61.    0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
  62.    0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
  63.    0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
  64.    0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
  65.    0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
  66.    0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
  67.    0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
  68.    0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
  69.    0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
  70.    0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
  71.    0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
  72.    0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
  73.    0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
  74.    0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
  75.    0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
  76.    0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
  77.    0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
  78.    0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
  79.    0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
  80.    0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
  81.    0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
  82.    0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
  83.    0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
  84.    0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
  85.    0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
  86.    0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
  87. };
  88.  
  89. Float Chaos(), Marble();
  90.  
  91. void
  92. InitTextureTable()
  93. {
  94.     int i, j, temp;
  95.  
  96.     seednrand(1);
  97.     hashTable = (short *) Malloc(4096*sizeof(short int));
  98.     for (i = 0; i < 4096; i++)
  99.         hashTable[i] = i;
  100.     for (i = 4095; i > 0; i--) {
  101.         j = (int)(nrand() * 4096);
  102.         temp = hashTable[i];
  103.         hashTable[i] = hashTable[j];
  104.         hashTable[j] = temp;
  105.     }
  106. }
  107.  
  108. void
  109. NoiseInit()
  110. {
  111.     int i;
  112.     Vector rp;
  113.  
  114.     InitTextureTable();
  115.  
  116.     for (i = 0; i < MAXSIZE; i++) {
  117.            rp.x = rp.y = rp.z = (Float)i;
  118.            RTable[i] = R(&rp)*REALSCALE - 1.0;
  119.      }
  120. }
  121.  
  122. static int
  123. R(v)
  124. Vector *v;
  125. {
  126.     v->x *= .12345;
  127.     v->y *= .12345;
  128.     v->z *= .12345;
  129.  
  130.     return Crc16(v, sizeof(Vector));
  131. }
  132.  
  133. /*
  134.  * Note that passing a Float to Crc16 and interpreting it as
  135.  * an array of chars means that machines with different floating-point
  136.  * representation schemes will evaluate Noise(point) differently.
  137.  */
  138. static int
  139. Crc16(buf, count)
  140. register char *buf;
  141. register int  count;
  142. {
  143.     register unsigned int crc = 0;
  144.  
  145.     while (count--)
  146.         crc = (crc >> 8) ^ xtab[ (unsigned char) (crc ^ *buf++) ];
  147.  
  148.     return crc;
  149. }
  150.  
  151.  
  152. /*
  153.  * Robert Skinner's Perlin-style "Noise" function
  154.  */
  155. Float
  156. Noise3(point)
  157. Vector *point;
  158. {
  159.     register int    ix, iy, iz, jx, jy, jz;
  160.     Float        x, y, z;
  161.     Float    sx, sy, sz, tx, ty, tz;
  162.     Float    sum;
  163.     short    m;
  164.  
  165.  
  166.     /* ensures the values are positive. */
  167.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  168.  
  169.     /* its equivalent integer lattice point. */
  170.     ix = (int)x; iy = (int)y; iz = (int)z;
  171.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  172.  
  173.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  174.  
  175.     /* the complement values of sx,sy,sz */
  176.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  177.  
  178.     /*
  179.      *  interpolate!
  180.      */
  181.     m = Hash3d( ix, iy, iz ) & 0xFF;
  182.     sum = INCRSUM(m,(tx*ty*tz),(x-ix),(y-iy),(z-iz));
  183.  
  184.     m = Hash3d( jx, iy, iz ) & 0xFF;
  185.     sum += INCRSUM(m,(sx*ty*tz),(x-jx),(y-iy),(z-iz));
  186.  
  187.     m = Hash3d( ix, jy, iz ) & 0xFF;
  188.     sum += INCRSUM(m,(tx*sy*tz),(x-ix),(y-jy),(z-iz));
  189.  
  190.     m = Hash3d( jx, jy, iz ) & 0xFF;
  191.     sum += INCRSUM(m,(sx*sy*tz),(x-jx),(y-jy),(z-iz));
  192.  
  193.     m = Hash3d( ix, iy, jz ) & 0xFF;
  194.     sum += INCRSUM(m,(tx*ty*sz),(x-ix),(y-iy),(z-jz));
  195.  
  196.     m = Hash3d( jx, iy, jz ) & 0xFF;
  197.     sum += INCRSUM(m,(sx*ty*sz),(x-jx),(y-iy),(z-jz));
  198.  
  199.     m = Hash3d( ix, jy, jz ) & 0xFF;
  200.     sum += INCRSUM(m,(tx*sy*sz),(x-ix),(y-jy),(z-jz));
  201.  
  202.     m = Hash3d( jx, jy, jz ) & 0xFF;
  203.     sum += INCRSUM(m,(sx*sy*sz),(x-jx),(y-jy),(z-jz));
  204.  
  205.     return sum;
  206.  
  207. }
  208.  
  209. /*
  210.  * Vector-valued "Noise"
  211.  */
  212. void
  213. DNoise3(point, result)
  214. Vector *point, *result;
  215. {
  216.     register int    ix, iy, iz, jx, jy, jz;
  217.     Float        x, y, z;
  218.     Float px, py, pz, s;
  219.     Float    sx, sy, sz, tx, ty, tz;
  220.     short    m;
  221.  
  222.     /* ensures the values are positive. */
  223.     x = point->x - MINX; y = point->y - MINY; z = point->z - MINZ;
  224.  
  225.     /* its equivalent integer lattice point. */
  226.     ix = (int)x; iy = (int)y; iz = (int)z;
  227.     jx = ix+1; jy = iy + 1; jz = iz + 1;
  228.  
  229.     sx = SCURVE(x - ix); sy = SCURVE(y - iy); sz = SCURVE(z - iz);
  230.  
  231.     /* the complement values of sx,sy,sz */
  232.     tx = 1.0 - sx; ty = 1.0 - sy; tz = 1.0 - sz;
  233.  
  234.     /*
  235.      *  interpolate!
  236.      */
  237.     m = Hash3d( ix, iy, iz ) & 0xFF;
  238.     px = x-ix;  py = y-iy;  pz = z-iz;
  239.     s = tx*ty*tz;
  240.     result->x = INCRSUM(m,s,px,py,pz);
  241.     result->y = INCRSUM(m+4,s,px,py,pz);
  242.     result->z = INCRSUM(m+8,s,px,py,pz);
  243.  
  244.     m = Hash3d( jx, iy, iz ) & 0xFF;
  245.     px = x-jx;
  246.     s = sx*ty*tz;
  247.     result->x += INCRSUM(m,s,px,py,pz);
  248.     result->y += INCRSUM(m+4,s,px,py,pz);
  249.     result->z += INCRSUM(m+8,s,px,py,pz);
  250.  
  251.     m = Hash3d( jx, jy, iz ) & 0xFF;
  252.     py = y-jy;
  253.     s = sx*sy*tz;
  254.     result->x += INCRSUM(m,s,px,py,pz);
  255.     result->y += INCRSUM(m+4,s,px,py,pz);
  256.     result->z += INCRSUM(m+8,s,px,py,pz);
  257.  
  258.     m = Hash3d( ix, jy, iz ) & 0xFF;
  259.     px = x-ix;
  260.     s = tx*sy*tz;
  261.     result->x += INCRSUM(m,s,px,py,pz);
  262.     result->y += INCRSUM(m+4,s,px,py,pz);
  263.     result->z += INCRSUM(m+8,s,px,py,pz);
  264.  
  265.     m = Hash3d( ix, jy, jz ) & 0xFF;
  266.     pz = z-jz;
  267.     s = tx*sy*sz;
  268.     result->x += INCRSUM(m,s,px,py,pz);
  269.     result->y += INCRSUM(m+4,s,px,py,pz);
  270.     result->z += INCRSUM(m+8,s,px,py,pz);
  271.  
  272.     m = Hash3d( jx, jy, jz ) & 0xFF;
  273.     px = x-jx;
  274.     s = sx*sy*sz;
  275.     result->x += INCRSUM(m,s,px,py,pz);
  276.     result->y += INCRSUM(m+4,s,px,py,pz);
  277.     result->z += INCRSUM(m+8,s,px,py,pz);
  278.  
  279.     m = Hash3d( jx, iy, jz ) & 0xFF;
  280.     py = y-iy;
  281.     s = sx*ty*sz;
  282.     result->x += INCRSUM(m,s,px,py,pz);
  283.     result->y += INCRSUM(m+4,s,px,py,pz);
  284.     result->z += INCRSUM(m+8,s,px,py,pz);
  285.  
  286.     m = Hash3d( ix, iy, jz ) & 0xFF;
  287.     px = x-ix;
  288.     s = tx*ty*sz;
  289.     result->x += INCRSUM(m,s,px,py,pz);
  290.     result->y += INCRSUM(m+4,s,px,py,pz);
  291.     result->z += INCRSUM(m+8,s,px,py,pz);
  292. }
  293.